package com.mm.controller.web.user;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.AopLog;
import com.mm.base.annotation.VerifyToken;
import com.mm.util.result.DPage;
import com.mm.util.result.DResult;
import com.mm.domain.user.DLevelInfo;
import com.mm.serivce.user.IDLevelInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * VIp等级表 前端控制器
 * </p>
 *
 * @author JueDream
 * @since 2021-08-04
 */
@Api(value = "/le/v1", tags = {"VIp等级"})
@Slf4j
@VerifyToken
@RestController
@RequestMapping("/le/v1")
@AopLog(type = "VIp等级", stackTraceOnErr = true)
public class DLevelInfoController {

    private final IDLevelInfoService levelInfoService;

    public DLevelInfoController(IDLevelInfoService levelInfoService) {
        this.levelInfoService = levelInfoService;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "page", value = ""),
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "limit", value = ""),
            @ApiImplicitParam(paramType = "query", dataType = "string", dataTypeClass = String.class, name = "levelText", value = "")
    })
    @ApiOperation(value = "VIp等级列表查询接口", notes = "", httpMethod = "POST")
    @PostMapping("/queryLevelPage")
    public DResult queryLevelPage(@RequestParam int page, @RequestParam int limit, @RequestParam(required = false) String levelText){
        Page<DLevelInfo> levelInfoPage = levelInfoService.queryLevelPage(page, limit, levelText);
        DPage<DLevelInfo> levelInfoDPage = DPage.to(levelInfoPage);
        return DResult.of(levelInfoDPage);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "levelId", value = "")
    })
    @ApiOperation(value = "VIp等级列表查询单个接口", notes = "", httpMethod = "POST")
    @PostMapping("/queryLevelOne")
    public DResult queryLevelOne(@RequestParam Integer levelId){
        return DResult.of(levelInfoService.queryLevelOne(levelId));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "DLevelInfo", dataTypeClass = DLevelInfo.class, name = "levelInfo", value = "")
    })
    @ApiOperation(value = "VIp等级列表编辑接口", notes = "", httpMethod = "POST")
    @Transactional
    @PostMapping("/saveOrUpdate")
    public DResult saveOrUpdate(@RequestBody DLevelInfo levelInfo) {
        boolean flag = false;
        if (levelInfo.getLevelId() < 0) {
            // 新增
            flag = levelInfoService.save(levelInfo);
        } else if (levelInfo.getLevelId()  > 0) {
            // 更新
            flag = levelInfoService.updateById(levelInfo);
        }
        return DResult.of(flag);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "levelId", value = "")
    })
    @ApiOperation(value = "VIp等级删除接口", notes = "", httpMethod = "DELETE")
    @DeleteMapping("/delete")
    public DResult delete(@RequestParam Integer levelId){
        boolean flag = false;
        if (levelId > 0) {
            flag = levelInfoService.removeLevel(levelId);
        }
        return DResult.of(flag);
    }

}
